const textBounce = (title, pageTxt, callBack1, callBack2) => {
	var timer = null;
	if (null != timer) {
		clearInterval(timer);
	}
	var showText = '';
	var i = 0;
	timer = setInterval(function() {
		showText = pageTxt.substring(0, i);
		callBack1(showText.replace(new RegExp(title), '<span style="color: rgba(208, 57, 86, 1);">' + title + '</span>'));
		i++;
		if (showText == pageTxt) {
			clearInterval(timer);
			callBack2();
		};
	}, 30);
}
// 倒计时
const countDownTime = (expiryDate, countDownEnd) => {
	var expDate = expiryDate;
	var nowDate = new Date().getTime();
	var cdDate = new Date(expDate - nowDate);
	if (cdDate > 0) {
		var days = parseInt(cdDate / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
		var hours = parseInt(cdDate / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
		var minutes = parseInt(cdDate / 1000 / 60 % 60, 10); //计算剩余的分钟 
		var seconds = parseInt(cdDate / 1000 % 60, 10); //计算剩余的秒数
		days = checkTime(days);
		hours = checkTime(hours);
		minutes = checkTime(minutes);
		seconds = checkTime(seconds);
		countDownEnd(days + ":" + hours + ":" + minutes + ":" + seconds);
	} else {
		countDownEnd("00:00:00:00");
	}

	var timer = setInterval(() => {
		var nowDate = new Date().getTime();
		var cdDate = new Date(expDate - nowDate);
		if (cdDate > 0) {
			var days = parseInt(cdDate / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
			var hours = parseInt(cdDate / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
			var minutes = parseInt(cdDate / 1000 / 60 % 60, 10); //计算剩余的分钟 
			var seconds = parseInt(cdDate / 1000 % 60, 10); //计算剩余的秒数

			days = checkTime(days);
			hours = checkTime(hours);
			minutes = checkTime(minutes);
			seconds = checkTime(seconds);
			countDownEnd(days + ":" + hours + ":" + minutes + ":" + seconds);
			cdDate -= 1000;
		} else {
			countDownEnd("00:00:00:00");
			clearInterval(timer);
		}
	}, 1000)
}
//将0-9的数字前面加上0，例1变为01 
const checkTime = (i) => {
	if (i < 10) {
		i = "0" + i;
	}
	return i.toString();
}
// 数字转大写字母
const numToCode = (num) => {
	return String.fromCharCode(num + 65)
}
const countDownSecond = (seconds, countDownSecondEnd) => {
	var secondTimer = setInterval(() => {
		seconds--
		countDownSecondEnd(seconds);
		if (seconds == 0) {
			clearInterval(secondTimer);
		}
	}, 1000)
}
const showToast = (toastText) => {
	uni.showToast({
		icon: 'none',
		title: toastText,
	})
}
module.exports = {
	textBounce: textBounce,
	countDownTime: countDownTime,
	numToCode: numToCode,
	countDownSecond: countDownSecond,
	showToast: showToast
}
